package org.mobicents.servlet.sip.example;



import java.util.concurrent.CopyOnWriteArrayList;


public class AdressBook {

	// adres + grupa
	private static CopyOnWriteArrayList<CUser> addressBook = new CopyOnWriteArrayList<CUser>();	
	
	public synchronized static void addCUser(CUser cUser)
	{
		if(addressBook.isEmpty())
			addressBook.add(cUser);
		else
		{
			for (CUser registeredUser : addressBook) {
				if(registeredUser.getLogin().equals(cUser.getLogin()))
				{
					System.out.println("CUser " + cUser.getLogin() + " already registered");
					return;
				}
			}	
			System.out.println("Adding new CUser " + cUser.getLogin());
			addressBook.add(cUser);
		}
	}
	
	public synchronized static void removeCUser(String login)
	{
		for (CUser cUser : addressBook) {
			System.out.println("login " + login + " -> " + cUser.getLogin());
			if(cUser.getLogin().equals(login)) {
				addressBook.remove(cUser);
				System.out.println("CUser " + cUser.getLogin() + " removed"); 
				break;
			}
		}
		System.out.println("No such user " + login + " to be removed");
	}
	
	public synchronized static CUser getCUser(String login)
	{
		for (CUser cUser : addressBook) {
			if(cUser.getLogin().equals(login)) {
				return cUser;
			}
		}
		return null;
	}
	
	public synchronized static CopyOnWriteArrayList<CUser> getList() {
		return addressBook;
	}
}
